package cn.sh.system.service.impl;

import cn.sh.basic.service.impl.BaseServiceImpl;
import cn.sh.system.domain.Role;
import cn.sh.system.mapper.RoleMapper;
import cn.sh.system.service.IRoleService;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sh
 * @since 2022-08-14
 */
@Service
public class RoleServiceImpl<id> extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    //查询所有信息
    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public Role selectOne(Long id) {
        return roleMapper.selectOne(id);
    }

    //添加信息
    @Override
    @Transactional
    public void add(Role Role) {
        roleMapper.add(Role);
        initRelation(Role.getId(),Role.getMenus(),Role.getPermissions());
        this.update(Role);
    }

    //修改信息
    @Override
    @Transactional
    public void update(Role Role) {
      
        roleMapper.update(Role);
    }

    //删除信息
    @Override
    @Transactional
    public void delete(Long id) {
        roleMapper.delete(id);
    }

    @Override
    @Transactional
    public void batchDelete(List ids) {
        roleMapper.batchDelete(ids);
    }


    private void initRelation(long id,List<Long> menus ,List<Long> permissions){

        //删除role_menu表中roleid对应的数据
        roleMapper.deleteRoleMenuByRoleId(id);
        //删除role_permission表中roleid对应的数据
        roleMapper.deleteRolePermissionByRoleId(id);
        //保存role_menu
        roleMapper.addRoleMenu(id,menus);
        //保存role_permission
        roleMapper.addRolePermission(id,permissions);
    }
}
